package space.edge.d.demo.qrcode.privatecode.encoder;

import space.edge.d.demo.qrcode.common.BitArray;

/**
 * Created by gsl on 2017/10/31.
 */

public class PMaskUtil {

    public static BitArray maskData(BitArray data, int maskPatternIndex) {
        BitArray maskedData = new BitArray(data.getSize());
        for (int index = 0; index < data.getSize(); index++) {
            boolean bit = data.get(index);
            bit ^= getDataMaskBit(index, maskPatternIndex);
            if (bit) maskedData.set(index);
        }
        return maskedData;
    }

    private static boolean getDataMaskBit(int index, int maskPatternIndex) {
        int intermediate;
        switch (maskPatternIndex) {
            case 0:
                intermediate = (index + 1) % 10 == 0 ? 0 : ((index - 2) / 5) % 2;
                break;
            case 1:
                intermediate = 0;
                break;
            default:
                throw new IllegalArgumentException();
        }
        return intermediate == 0;
    }

}